/*****************************************************************************
 * led_aip1638.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "cal/protocol/dio/plum_protocol_dio.h"
#include "hardware/display/display.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_NONE
#define LOG_MODULE    "dev.led_display"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

//数据指令
#define AIP1638_DATA_CMD_MSB 0x40 /** 数据指令高位 */

#define AIP1638_DATA_BIT_WRITE_DISPREG 0 /** 写显存 */
#define AIP1638_DATA_BIT_READ_KEY      2 /** 读键扫 */

#define AIP1638_DATA_BIT_AUTOINC_ADDR 0 /** 地址自增 */
#define AIP1638_DATA_BIT_STABLE_ADDR  3 /** 固定地址 */

#define AIP1638_DATA_BIT_MODE_NORM 0 /** 普通模式 */
#define AIP1638_DATA_BIT_MODE_FACT 8 /** 工厂模式 */

//地址指令
#define AIP1638_ADDR_CMD_MSB 0x60 /** 地址指令高位 */

//显示指令
#define AIP1638_DISP_CMD_MSB 0x80 /** 显示指令高位 */

#define AIP1638_DISP_BIT_DISP_ON  8 /** 开显示 */
#define AIP1638_DISP_BIT_DISP_OFF 0 /** 关显示 */

#define AIP1638_DISP_BIT_BAKLIGHT_16_1  0 /** 1/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_2  1 /** 2/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_4  2 /** 4/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_10 3 /** 10/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_11 4 /** 11/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_12 5 /** 12/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_13 6 /** 13/16脉宽 */
#define AIP1638_DISP_BIT_BAKLIGHT_16_14 7 /** 14/16脉宽 */

//数字
#define AIP1638_DISP_NUMBER_0  0x3F
#define AIP1638_DISP_NUMBER_1  0x06
#define AIP1638_DISP_NUMBER_2  0x5B
#define AIP1638_DISP_NUMBER_3  0x4F
#define AIP1638_DISP_NUMBER_4  0x66
#define AIP1638_DISP_NUMBER_5  0x6D
#define AIP1638_DISP_NUMBER_6  0x7D
#define AIP1638_DISP_NUMBER_7  0x07
#define AIP1638_DISP_NUMBER_8  0x7F
#define AIP1638_DISP_NUMBER_9  0x6F
#define AIP1638_DISP_NUMBER_DG 0x80

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/**
 * @brief： 将待显示的值转换为AIP协议数字
 *
 * @param:  value     -   数字（0~10）
 *
 * @return:
 */
PLUM_PRIVATE
plum_u8 value2number(plum_u8 value)
{
    plum_u8 num = 0;

    struct tn {
        plum_u8 value;
        plum_u8 number;
    };
    struct tn[] = {
        {0, AIP1638_DISP_NUMBER_0},   {1, AIP1638_DISP_NUMBER_1},
        {2, AIP1638_DISP_NUMBER_2},   {3, AIP1638_DISP_NUMBER_3},
        {4, AIP1638_DISP_NUMBER_4},   {5, AIP1638_DISP_NUMBER_5},
        {6, AIP1638_DISP_NUMBER_6},   {7, AIP1638_DISP_NUMBER_7},
        {8, AIP1638_DISP_NUMBER_8},   {9, AIP1638_DISP_NUMBER_9},
        {10, AIP1638_DISP_NUMBER_DG},
    };

    plum_u8 loop;
    for (loop = 0; loop < PLUM_ARRAY_CNT(tn); loop++) {
        if (tn[loop].value == value) {
            num = tn[loop].number;
            break;
        }
    }

    return (num);
}

/**
 * @brief:  清屏
 * **/
PLUM_PRIVATE
plum_void dev_led_aip1638_show_clear(plum_void)
{
    plum_cal_protocol_dio_cmd_write_start();

    plum_cal_protocol_dio_cmd_write_byte(0x40);  //写普通模式，地址自增，显示
    plum_cal_protocol_dio_cmd_write_ack();

    plum_cal_protocol_dio_cmd_write_byte(0xC0);  //显示地址
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_ack();

    plum_cal_protocol_dio_cmd_write_byte(0x8F);  // 14/16显示亮度

    plum_cal_protocol_dio_cmd_write_end();

    plum_cal_protocol_dio_cmd_write_start();

    plum_cal_protocol_dio_cmd_write_byte(0x40);  //写普通模式，地址自增，显示
    plum_cal_protocol_dio_cmd_write_ack();

    plum_cal_protocol_dio_cmd_write_byte(0xC8);  //显示地址
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);     //显示数据
    plum_cal_protocol_dio_cmd_write_ack();

    plum_cal_protocol_dio_cmd_write_byte(0x8F);  // 14/16显示亮度

    plum_cal_protocol_dio_cmd_write_end();
}

/**
 * @brief:  显示数字值
 *
 * @param[in]:  x       -   x坐标
 * @param[in]:  y       -   y坐标
 * @param[in]:  fsz     -   值
 *
 * @return:
 * **/
PLUM_PRIVATE
plum_void dev_led_aip1638_value(plum_u32 x, plum_u32 y, plum_u32 value)
{
    plum_u32 tmp = value % 10000;
    plum_u8  c1  = value2number(tmp / 1000);
    tmp          = tmp % 1000;
    plum_u8 c2   = value2number(tmp / 100);
    tmp          = tmp % 100;
    plum_u8 c3   = value2number(tmp / 10);
    plum_u8 c4   = value2number(tmp % 10);

    plum_cal_protocol_dio_cmd_write_start();

    plum_cal_protocol_dio_cmd_write_byte(0x40);  //写普通模式，递增地址，显示
    plum_cal_protocol_dio_cmd_write_ack();

    if (x == 1) {
        plum_cal_protocol_dio_cmd_write_byte(0xC0);  //显示地址
    }
    else {
        plum_cal_protocol_dio_cmd_write_byte(0xC8);  //显示地址
    }
    plum_cal_protocol_dio_cmd_write_byte(c1);  //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);
    plum_cal_protocol_dio_cmd_write_byte(c2);  //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);
    plum_cal_protocol_dio_cmd_write_byte(c3);  //显示数据
    plum_cal_protocol_dio_cmd_write_byte(0);
    plum_cal_protocol_dio_cmd_write_byte(c4);  //显示数据
    plum_cal_protocol_dio_cmd_write_byte();

    plum_cal_protocol_dio_cmd_write_byte(0x8F);  // 14/16显示亮度
    plum_cal_protocol_dio_cmd_write_end();
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_display_init(plum_void)
{
    plum_s32 rc = PLUM_ECODE_OK;

    cal_protocol_dio_init_t cof = {
        .clk = PLUM_HAL_GPIO_ID(A, 1),
        .dio = PLUM_HAL_GPIO_ID(A, 2),
        .stb = PLUM_HAL_GPIO_ID(A, 3),
    };
    rc = plum_cal_protocol_dio_init(&cof);
    if (rc) {
        LOG_E("plum_hal_dio_init err,rc:%d", rc);
    }

    dev_led_aip1638_show_clear();

    return (rc);
}

PLUM_PUBLIC
plum_void plum_display_number(plum_display_pos_t pos, plum_u32 fsz,
                              plum_u32 number)
{
    dev_led_aip1638_value(pos.x, pos.y, number);
}

PLUM_PUBLIC
plum_void plum_display_clear(plum_void)
{
    dev_led_aip1638_show_clear();
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
